//
//  Comment.swift
//  App
//
//  Created by melvyn on 2018/9/1.
//

import Foundation
import Vapor
import Fluent

struct Comment: Codable {
    var id: Int?
    var content: String?
    var uid: User.ID?
    var liveId: LiveStream.ID
}

extension Comment: Entity {}
extension Comment: Migration {}
extension Comment: Content {}
extension Comment: Parameter {}
extension Comment {
    var live: Parent<Comment, LiveStream> {
        return parent(\.liveId)
    }
    
    var user: Parent<Comment, User> {
        return parent(\.uid!)
    }
}

extension Comment {
    struct Public: Codable {
        var id: Comment.ID
        var content: String?
        var user: User.Public
        var liveId: LiveStream.ID
    
        init(_ related: (Comment, User.Public)) {
            id = related.0.id!
            liveId = related.0.liveId
            content = related.0.content
            user = related.1
        }
    }
}
extension Comment.Public: Content {}

